package com.mediacenter.widget;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnKeyListener;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.TextView;

import com.mediacenter.R;

public class ProgressManager {
	 
	protected static ProgressManager instance;
	protected Activity mActivity;
	private Dialog progressDialog;
	private String mMessage;
	
	private ProgressManager() {}
	
	public static ProgressManager getInstance() {
		if (instance == null) {
			instance = new ProgressManager();
		}
		return instance;
	}
	
	public void setActivity(Activity activity) {
		if (mActivity == activity) {
			return;
		}
		this.mActivity = activity;
		if (progressDialog != null && progressDialog.isShowing()) {
			try {
				progressDialog.cancel();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public void showProcessDialog(String message) {
		try {
			this.mMessage = message;
			if (mActivity == null) {
				throw new Exception();
			}
			if (progressDialog != null) {
				progressDialog.cancel();
			}
			
			LayoutInflater layoutInflater = LayoutInflater.from(mActivity);
			View view = layoutInflater.inflate(R.layout.progress_dialog, null);
			TextView mMessageText = (TextView) view.findViewById(R.id.progress_msg_text);
			
			if(message != null){
				mMessageText.setText(message);
			}
			progressDialog = new AlertDialog.Builder(mActivity).create();
			if (mOnKeyListener != null) {
				progressDialog.setOnKeyListener(mOnKeyListener);
			} else {
				progressDialog.setOnKeyListener(new OnKeyListener() {
					@Override
					public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
						if (keyCode == KeyEvent.KEYCODE_BACK) {
							if (progressDialog != null) {
								progressDialog.cancel();
							}
						}
						return false;
					}
				});
			}
			progressDialog.setOnCancelListener(new OnCancelListener() {
				@Override
				public void onCancel(DialogInterface dialog) {
					if (mOnCancelListener != null) {
						mOnCancelListener.onCancel(dialog);
					}
				}
			});
			progressDialog.setCanceledOnTouchOutside(false);
			((Dialog) progressDialog).show();
			progressDialog.setContentView(view,new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void show() {
		if (progressDialog != null) {
			progressDialog.show();
		}else if (mMessage != null) {
			showProcessDialog(mMessage);
		}
	}

	public boolean isDismiss() {
		return progressDialog == null||!progressDialog.isShowing();
	}
	
	public void cancel() {
		if (mActivity.isFinishing()) {
			return;
		}
		if (progressDialog!=null) {
			progressDialog.cancel();
			progressDialog = null;
		}
	}
	
	private OnCancelListener mOnCancelListener;
	
	private OnKeyListener mOnKeyListener;
	
	public void setOnCancelListener(final OnCancelListener onCancelListener) {
		mOnCancelListener = onCancelListener;
	}

	public void setOnKeyListener(OnKeyListener mOnKeyListener) {
		this.mOnKeyListener = mOnKeyListener;
	}

}
